package com.wd.cloud.applet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wd.cloud.applet.common.constant.dataverify.Constant;
import com.wd.cloud.applet.common.exception.CustomException;
import com.wd.cloud.applet.config.CustomListResultConfig;
import com.wd.cloud.applet.config.ModelMapperConfig;
import com.wd.cloud.applet.config.MybatisPlusPageConfig;
import com.wd.cloud.applet.entity.bo.SysMenuInfoBO.SysMenuInfoListBO;
import com.wd.cloud.applet.entity.bo.SysMenuInfoBO.SysMenuInfoSaveBO;
import com.wd.cloud.applet.entity.commons.RequestPrimaryKeyModel;
import com.wd.cloud.applet.entity.dto.SysMenuInfoModel;
import com.wd.cloud.applet.entity.dto.SysRoleInfoModel;
import com.wd.cloud.applet.entity.vo.SysMenuInfoVO.SysMenuInfoByListVO;
import com.wd.cloud.applet.entity.vo.SysMenuInfoVO.SysMenuInfoKeyVO;
import com.wd.cloud.applet.entity.vo.SysMenuInfoVO.SysMenuInfoListVO;
import com.wd.cloud.applet.entity.vo.SysMenuInfoVO.SysMenuInfoPageListVO;
import com.wd.cloud.applet.entity.vo.SysMenuInfoVO.SysMenuRoleListVO;
import com.wd.cloud.applet.mapper.SysMenuInfoMapper;
import com.wd.cloud.applet.mapper.SysRoleInfoMapper;
import com.wd.cloud.applet.service.ISysMenuInfoService;
import com.wd.cloud.applet.utils.HelperUtils;
import com.wd.cloud.applet.utils.string.StringUtils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
* Description: [菜单表服务实现]
* Created on 2023年04月21日
* @author  <a href="mailto: 987707681@qq.com">hjx</a>
* @version 1.0
* Copyright (c) 2023年 代码生成器
*/
@Service
public class SysMenuInfoServiceImpl extends ServiceImpl<SysMenuInfoMapper, SysMenuInfoModel> implements ISysMenuInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SysMenuInfoMapper appletMapper;
    @Resource
    private SysRoleInfoMapper roleInfoMapper;
    /**
     * 根据主键ID删除数据（逻辑删除）
     *
     * @param params
     * @return
     */
    @Override
    public int deleteByPrimaryKey(RequestPrimaryKeyModel params) {
        if (StringUtils.isNullOrTrimEmpty(params.getId())) {
            throw new CustomException("请选择需要删除的数据", 100);
        }
        SysMenuInfoModel model = ModelMapperConfig.mapTo(SysMenuInfoModel.class,params);
        if (model == null) {
            throw new CustomException("删除的数据不存在", 100);
        }
        return appletMapper.deleteById(model);
    }

    /**
     * 新增数据
     *
     * @param params
     * @return
     */
    @Override
    public int insertSelective(SysMenuInfoSaveBO params) {
        SysMenuInfoModel model = ModelMapperConfig.mapTo(SysMenuInfoModel.class, params);
        model.setId(HelperUtils.getUuid());
        model.setCreateTime(new Date());
        return appletMapper.insert(model);
    }

    /**
     * 新增或修改
     *
     * @param params
     * @return
     */
    @Override
    public int save(SysMenuInfoSaveBO params) {
        SysMenuInfoModel model = ModelMapperConfig.mapTo(SysMenuInfoModel.class, params);
        if (model != null) {
            if (model.getId() == null || model.getId().isEmpty()) {
                model.setId(HelperUtils.getUuid());
                model.setCreateTime(new Date());
                model.setDeleteMark(0);
                model.setStatus(0);
                return appletMapper.insert(model);
            } else {
                return appletMapper.updateById(model);
            }
        }
        return 0;
    }

    /**
     * 分页获取数据列表
     *
     * @param params
     * @return
     */
    @Override
    public CustomListResultConfig<SysMenuInfoListVO> selectByPageList(SysMenuInfoListBO params) {
        LambdaQueryWrapper<SysMenuInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.orderByDesc(SysMenuInfoModel::getCreateTime);
        Page<SysMenuInfoModel> page = new Page<>(params.getPage(), params.getLimit());
        IPage<SysMenuInfoModel> entityIPage = appletMapper.selectPage(page, userLambdaQueryWrapper);
        CustomListResultConfig<SysMenuInfoListVO> responseDtoList =
            MybatisPlusPageConfig.toPageResult(entityIPage, SysMenuInfoListVO.class);
        if (responseDtoList.getList().size() <= 0) {
            throw new CustomException("查无数据", 102);
        }
        return responseDtoList;
    }
    /**
     * 分页获取数据列表
     *
     * @param
     * @return
     */
    @Override
    public List<SysMenuInfoPageListVO> selectByList() {
        LambdaQueryWrapper<SysMenuInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.orderByAsc(SysMenuInfoModel::getSort);
        List<SysMenuInfoModel> entityIPage = appletMapper.selectList(userLambdaQueryWrapper);
        List<SysMenuInfoPageListVO> responseDtoList =
            ModelMapperConfig.mapList(entityIPage, SysMenuInfoPageListVO.class);
        return responseDtoList;
    }
    /**
     * 分页获取数据列表
     *
     * @param
     * @return
     */
    @Override
    public List<SysMenuInfoByListVO> getMenuList() {
        LambdaQueryWrapper<SysMenuInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.orderByAsc(SysMenuInfoModel::getSort);
        List<SysMenuInfoModel> entityIPage = appletMapper.selectList(userLambdaQueryWrapper);
        List<SysMenuInfoByListVO> responseDtoList =
            ModelMapperConfig.mapList(entityIPage, SysMenuInfoByListVO.class);
        return responseDtoList;
    }
    /**
     * 根据主键id获取实体
     *
     * @param params
     * @return
     */
    @Override
    public SysMenuInfoKeyVO selectByPrimaryKey(RequestPrimaryKeyModel params) {
        if (params.getId() == null || params.getId().isEmpty()) {
            throw new CustomException("Id不能为空", 100);
        }
        SysMenuInfoModel model = appletMapper.selectById(params.getId());
        SysMenuInfoKeyVO responestDto = ModelMapperConfig.mapTo(SysMenuInfoKeyVO.class, model);
        if (responestDto == null) {
            throw new CustomException("Id错误", 100);
        }
        return responestDto;
    }
    /**
     * 根据主键id获取实体
     *
     * @param params
     * @return
     */
    @Override
    public SysMenuRoleListVO menuRoleList(RequestPrimaryKeyModel params) {
        if (params.getId() == null || params.getId().isEmpty()) {
            throw new CustomException("Id不能为空", 100);
        }
        SysRoleInfoModel model = roleInfoMapper.selectById(params.getId());
        RequestPrimaryKeyModel keyModel=new RequestPrimaryKeyModel();
        keyModel.setId(model.getId());
        ArrayList arrayList=new ArrayList<>();
        List<SysMenuInfoListVO> listVOList=appletMapper.queryMuchTable(keyModel);
        for (SysMenuInfoListVO listVO:listVOList){
            arrayList.add(listVO.getId());
        }
        int size=arrayList.size();
        String[] array = (String[])arrayList.toArray(new String[size]);
        SysMenuRoleListVO listVO=new SysMenuRoleListVO();
        listVO.setMenu(array);
        return listVO;
    }
}
